package com.eastcom.collection.distributor


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * 队列消息，用于队列客户端和队列服务通信。 消息主要由三部分信息组成: type: 消息类型 content: 消息内容 properties: 消息参数
 *
 * @author 张海峰
 * @version 1.0
 */
public class QueueMessage implements Serializable {
    //~ Static fields/initializers ---------------------------------------------

    /** 读取请求 */
    public static final int MSG_READ_REQ = 0x00000001;

    /** 写入请求 */
    public static final int MSG_WRITE_REQ = 0x00000002;

    /** 补读请求 */
    public static final int MSG_EXPIRE_READ_REQ = 0x00000003;

    /** 区间读请求 */
    public static final int MSG_MISSED_READ_REQ = 0x00000004;

    /** 读取响应 */
    public static final int MSG_READ_ACK = 0x00001001;

    /** 写入响应 */
    public static final int MSG_WRITE_ACK = 0x00001002;

    /** 补读响应 */
    public static final int MSG_EXPIRE_READ_ACK = 0x00001003;

    /** 区间读响应 */
    public static final int MSG_MISSED_READ_ACK = 0x00001004;
    
    static final long serialVersionUID = 19791225000000L;

    //~ Instance fields --------------------------------------------------------

    private ArrayList content;
    private HashMap   properties;
    private int       type = 0;

    //~ Constructors -----------------------------------------------------------

    /**
     * Creates a new QueueMessage object.
     *
     * @param type DOCUMENT ME!
     */
    public QueueMessage(int type) {
        this.type = type;
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * DOCUMENT ME!
     *
     * @param content DOCUMENT ME!
     */
    public void setContent(ArrayList content) {
        this.content = content;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public ArrayList getContent() {
        return this.content;
    }

    /**
     * DOCUMENT ME!
     *
     * @param properties DOCUMENT ME!
     */
    public void setProperties(HashMap properties) {
        this.properties = properties;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public HashMap getProperties() {
        return this.properties;
    }

    /**
     * DOCUMENT ME!
     *
     * @param type DOCUMENT ME!
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getType() {
        return this.type;
    }

    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static void main(String[] args) {
        QueueMessage ui = new QueueMessage(1);
        ArrayList    al = new ArrayList();
        al.add("test");
        al.add("testmmx");
        al.add("mmx");
        ui.setContent(al);

        HashMap hm = new HashMap();
        hm.put("pig", "dog");
        hm.put("gpig", "mdog");
        ui.setProperties(hm);

        try {
            //写入
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream    out = new ObjectOutputStream(bout);
            out.writeObject("msging serial");
            out.writeObject(ui);
            out.flush();

            ObjectInputStream inp = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
            String            s  = (String) inp.readObject();
            QueueMessage      qm = (QueueMessage) inp.readObject();
            System.out.println(s);
            qm.printMe();
        } catch (Exception e) {
        }
    }

    /**
     * DOCUMENT ME!
     */
    public void printMe() {
        System.out.println("message type is: " + type);

        if (content != null) {
            System.out.println("ArrayList is: " + content);
        } else {
            System.out.println("ArrayList is null ");
        }

        if (properties != null) {
            System.out.println("HashMap is: " + properties);
        } else {
            System.out.println("HashMap is null ");
        }
    }
}